# long 爬虫
# {2021/9/7}
# 本章目的：阿里妈妈
import requests
import re
import time
import json
addtime = int(time.time())

url = f"https://pub.alimama.com/openapi/json2/1/gateway.unionpub/optimus.material.json?t={addtime}000&_tb_token_=76eefe7f7b6bb"

print(url)

data = {
    "_data_":'{"floorId":"28030","pageNum":3,"pageSize":60,"refpid":"mm_55574459_0_0"}'
}
header = {
    "accept-encoding":"gzip, deflate, br",
    "accept-language": "zh-CN,zh;q=0.9",
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
    "cookie":"t=824aeb52894c5598eea27d8d3c5b892b; cookie2=1a7a0d47ca729b01a5af204ddf5c6fe0; v=0; _tb_token_=76eefe7f7b6bb; cna=nAC6GRanb2gCAbaVyKNBxJj5; _ga=GA1.1.800657225.1631025510; xlly_s=1; alimamapwag=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV09XNjQpIEFwcGxlV2ViS2l0LzUzNy4zNiAoS0hUTUwsIGxpa2UgR2Vja28pIENocm9tZS84Ni4wLjQyNDAuMTk4IFNhZmFyaS81MzcuMzY%3D; cookie32=d897988e14406c13e32675fa894dc43f; alimamapw=RHcDQXdWF3sIECMiFXFQEXdwRHAGQXdYOwgMUVFVUQRRBFQGVg0FB1BRUQsOBlYBAQRXUgICBVFV%0A; cookie31=NTU1NzQ0NTksJUU5JUFEJTk0JUU5JUJFJTk5JUU1JTg2JUFDJUU5JTlCJUFBLDE1ODIzNTE3OTkwQDE2My5jb20sVEI%3D; _ga_Q39CBKW296=GS1.1.1631025510.1.1.1631025534.0; taokeisb2c=; taokeIsBoutiqueSeller=eQ%3D%3D; account-path-guide-s1=true; JSESSIONID=B6F429D0E591E1A9AF63D84AD39A0A07; login=Vq8l%2BKCLz3%2F65A%3D%3D; x5sec=7b2268616c6c65792d736f6c61723b32223a226564663661633361353333626131353039353334643437336535366266333530434c574b336f6b47454e54483974696d35396d357477456f416a44496e5072522f762f2f2f2f3842227d; l=eBrTvne7gMqIEZaaBO5Zourza7790CRfCsPzaNbMiInca6gO_gkTvNCKrZoM8dtjgt52LetPkdi_7RUe7c4_WxwZ4JyWRrkKzQppRe1..; tfstk=c5nRB0ARkIAoO0yxQ4LDOA5elQMda-OLGTNhvLxAyhUySGIOfsbrjc3iXUwIu6pA.; isg=BPz8GrdHsbhXL4Vk_wv0eqtDzZqu9aAf7_CyutZ5m-Z_oZkr_gbxrT9XgcnZ6dh3",
    "origin":"https://pub.alimama.com",
    "referer":"https://pub.alimama.com/promo/search/index.htm?fn=618&toPage=2&pageSize=60",
    "sec-fetch-dest":"document",
    "sec-fetch-mode":"navigate",
    "sec-fetch-site": "same-origin",
    "sec-fetch-user":"?1",
    "upgrade-insecure-requests":"1",
}
reque = requests.post(url,params=data,headers=header,verify=True)
content = reque.text
obj = json.loads(content)

with open('taobao.txt','a',encoding='utf-8') as f:
    for it in obj['model']['recommend']['resultList']:
        text = '名字：'+it['itemName']+'\n'+\
               '价格：'+it['promotionPrice']+'\n'+\
               '优惠价格：'+it['priceAfterCoupon']+'\n'+ \
               '佣金：'+it['tkCommissionAmount']+'\n'+ \
               '推广地址：'+ it['couponShareUrl']+'\n'

        f.writelines(text)

f.close()
print('ok')


